﻿using MVC5Test.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC5Test.Controllers
{
    /// <summary>
    /// 后端向前端传递数据的方式：
    /// ViewData
    /// ViewBag
    /// TempData
    /// Model
    /// 通过看源码发现实际上都是通过ViewData实现的。
    /// </summary>
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            //从控制器向页面传递数据 ViewBag 的本质就是ViewData
            //1.ViewBag
            ViewBag.Name = "zhaichao";
            //2.ViewData
            ViewData["Age"] = "29";
            return View();
        }

        /// <summary>
        /// TempData 可以跨页面传递数据，但是只能读取一次。
        /// </summary>
        public void Demo1()
        {
            TempData.Add("UserName", "chao");
            Response.Redirect("~/Home/Demo2");
        }

        public ActionResult Demo2()
        {
            return View();
        }
        
        /// <summary>
        /// 通过Model传给页面数据
        /// </summary>
        /// <returns></returns>

        public ActionResult ShowData()
        {
            //return View(new CurrentUser() { Age = 20, Id = 1, Name = "zhai chao1" });
            return View("ShowData2", "_Layout2", new CurrentUser() { Age = 20, Id = 1, Name = "zhai chao1" });
        }
    }
}